package com.hly.musicbackground.model.movies.service.impl;

import com.hly.musicbackground.Final.MoviesFromFinal;
import com.hly.musicbackground.Utils.StringUtils;
import com.hly.musicbackground.domain.Movies;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class MoviesBILIBILI {

    public List<Movies> searchDomToObject(Document document) {
        ArrayList<Movies> movies = new ArrayList<>();

//        Element main = document.select(".layout-main").get(2);

        Elements infos = document.select("div.bangumi-pgc-list.i_wrapper.search-all-list div.media-item .media-item-col");

        infos.stream().forEach(n -> {
            if (n.hasClass("to_hide_md")){
                return;
            }
//            System.out.println(infos.select(".qy-mod-link-wrap a.qy-mod-link").attr("href"));
            if (infos.select(".qy-mod-link-wrap a.qy-mod-link").attr("href").indexOf("www.iqiyi.com") != -1){
                Movies mov = new Movies();

                // 获取影视封面
                mov.setImage(n.select(".qy-mod-link-wrap a.qy-mod-link img.qy-mod-cover").attr("src"));


                //设置来源
                mov.setResourcesFrom(MoviesFromFinal.IQIYI);

                // 获取默认链接
                mov.setUrl(n.select(".qy-mod-link-wrap a.qy-mod-link").attr("href"));

                // 获取影视名称
                mov.setMoviesName(n.select(".qy-mod-link-wrap a.qy-mod-link").attr("title"));


                // 获取影视类型
                String type = n.select("div.result-right h3.qy-search-result-tit span.item-type").text();
                if (StringUtils.isNotEmpty(type)){
                    mov.setType(type);
                }

                // 获取其他信息
                //mov.setSub();

                // 获取英文名


                n.select("div.qy-search-result-info.half").stream().forEach(m -> {
                    if ("导演:".equals(m.select("label.info-lbl").text())){
                        // 获取导演
                        mov.setDirect(m.select("span").text());
                    }else {
                        // 获取主演
                        mov.setActLeadingRole(m.select("span").text());
                    }
                });

                // 获取简介
                Elements Description = n.select(".qy-search-result-info.multiple span.info-des");
                if (Description.size() <= 1){
                    mov.setDescription(Description.attr("title"));
                }else{
                    mov.setDescription(Description.get(1).attr("title"));
                }

                movies.add(mov);
            }
        });
        System.out.println(document);
        return movies;
    }
}
